﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CrossLayerCommunication.FluentValidation;
using ExampleWebApplication.DAL;
using ExampleWebApplication.Messages;
using ExampleWebApplication.Models;
using CrossLayerCommunication;

namespace ExampleWebApplication.BLL
{
    public static class TaskService
    {
        readonly static TimeSpan StartWorkTime = new TimeSpan(9,0,0);
        readonly static TimeSpan EndWortkTime = new TimeSpan(17,0,0);

        public static IOperationResult<IEnumerable<Task>> GetTaskForToday()
        {
            DateTime actualTime = DateTime.Now;

            #region BLL Validation
            var validateDayAndWorkTime = new OperationResult<IEnumerable<Task>>()
                .MergeFluentValidationResult
                (
                    actualTime.DayOfWeek.FluentValidation(AllMessages.Instance.TasksMessages.NoTasksForToday)
                        .IsNotEqualTo(DayOfWeek.Saturday)
                        .IsNotEqualTo(DayOfWeek.Sunday),
                    actualTime.TimeOfDay.FluentValidation(AllMessages.Instance.TasksMessages.NoTasksForToday)
                        .IsBetween(StartWorkTime, EndWortkTime)
                );

            if (!validateDayAndWorkTime.WasSuccessful)
                return validateDayAndWorkTime;
            #endregion

            //check if no task message
            if (validateDayAndWorkTime.Messages.Contains(AllMessages.Instance.TasksMessages.NoTasksForToday))
                return validateDayAndWorkTime;

            //get data from DAL
            var getTasksFromStorage = new Storage().GetTasks();


            //data retrieval was unsuccessful
            if (!getTasksFromStorage.WasSuccessful)
                return getTasksFromStorage;

            return new OperationResult<IEnumerable<Task>>(getTasksFromStorage.Data.Where(item => item.EndTime >= actualTime.TimeOfDay));
        }
    }
}